package com.ln.store.service.Impl;

import java.sql.SQLException;

import com.ln.store.dao.UserDao;
import com.ln.store.dao.Impl.UserDaoImpl;
import com.ln.store.domain.User;
import com.ln.store.service.UserService;

public class UserServiceImpl implements UserService {

	public void userRegister(User user) throws SQLException {

		// 实现注册功能
		UserDao dao = new UserDaoImpl();
		dao.userRegister(user);
	}

	public boolean userActive(String code) throws SQLException {
		// 实现注册功能
		UserDao dao = new UserDaoImpl();
		User user = dao.userActive(code);

		// 判断是否查找到该注册用户
		if (user != null) {
			// 修改用户状态，清空用户激活码
			user.setState(1);
			user.setCode(null);
			// 更新用户信息
			dao.updateUser(user);
			return true;
		} else { // 找不到该激活码
			return false;
		}
	}
	
	//此处用了抛异常的方式处理用户提示
	public User userLogin(User user) throws SQLException {
		// 实现登录功能
		UserDao dao = new UserDaoImpl();
		User user2 = dao.userLogin(user);
		
		//判断是否有该用户
		if (user2 == null) {
		   throw new RuntimeException("账号或密码错误");
		}else if(user2.getState() == 0) {
		   throw new RuntimeException("用户未激活");
		}else {
			return user2;
		}
	}

}
